import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame {
    final private JTextField usernameField;
    final private JPasswordField passwordField;
    final private JButton loginButton;
    final private JButton resetButton;
    final private JLabel statusLabel;

    public static void main(String[] args) {
        Main frame = new Main("登录窗口");
        frame.setSize(350, 220);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public Main(String title) {
        super(title);

        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;


        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("用户名:"), gbc);


        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 1.0;
        usernameField = new JTextField(15);
        mainPanel.add(usernameField, gbc);


        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("密码:"), gbc);


        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 1.0;
        passwordField = new JPasswordField(15);
        mainPanel.add(passwordField, gbc);


        JPanel buttonPanel = new JPanel(new FlowLayout());
        loginButton = new JButton("登录");
        resetButton = new JButton("重置");
        buttonPanel.add(loginButton);
        buttonPanel.add(resetButton);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.weightx = 0.0;
        mainPanel.add(buttonPanel, gbc);


        statusLabel = new JLabel(" ", SwingConstants.CENTER);
        statusLabel.setForeground(Color.blue);
        statusLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        mainPanel.add(statusLabel, gbc);

        add(mainPanel);

        addEventListeners();
    }

    private void addEventListeners() {
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                if (username.equals("张三") && password.equals("123456")) {
                    statusLabel.setText("登录成功");
                    statusLabel.setForeground(Color.blue);
                } else {
                    statusLabel.setText("用户名或密码错误");
                    statusLabel.setForeground(Color.red);
                }
            }
        });

        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                usernameField.setText("");
                passwordField.setText("");
                statusLabel.setText(" ");
            }
        });

        // 添加回车键登录功能
        usernameField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loginButton.doClick();
            }
        });

        passwordField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loginButton.doClick();
            }
        });
    }
}